﻿namespace Microsoft.AspNetCore.Http
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Abp.Json;
    using Microsoft.AspNetCore.Http;
    using Newtonsoft.Json;
    using FFF.Test.Entity.FW.View;
    using FFF.Test.Entity.WX.View;
    using FFF.Test.Util.Consts;
    using Ydm.Extensions;

    /// <summary>
    /// session的扩展
    /// </summary>
    public static class SessionExtensions
    {
        /// <summary>
        /// 设置用户缓存
        /// </summary>
        /// <param name="session">session</param>
        /// <param name="viewUser">用户信息</param>
        public static void SetUserInfo(this ISession session, ViewFWUser viewUser)
        {
            var strUserInfo = JsonSerializationHelper.SerializeWithType(viewUser);
            session.SetString(AppConfigConsts.UserInfo, strUserInfo);
        }

        /// <summary>
        /// 获取用户缓存
        /// </summary>
        /// <param name="session">session</param>
        /// <returns>用户session信息</returns>
        public static ViewFWUser GetUserInfo(this ISession session)
        {
            var userStr = session.GetString(AppConfigConsts.UserInfo);
            if (userStr.IsNullOrEmpty())
            {
                return null;
            }

            ViewFWUser viewFWUser = JsonSerializationHelper.DeserializeWithType<ViewFWUser>(userStr);
            return viewFWUser;
        }

        /// <summary>
        /// 移除用户缓存
        /// </summary>
        /// <param name="session">session</param>
        public static void RemoveUserInfo(this ISession session)
        {
            session.Remove(AppConfigConsts.UserInfo);
        }

        /// <summary>
        /// 获取微信用户缓存
        /// </summary>
        /// <param name="session">session</param>
        /// <returns>用户session信息</returns>
        public static ViewWXUserInfo GetWxUserInfo(this ISession session)
        {
            var userStr = session.GetString(AppConfigConsts.WxUserInfo);
            if (userStr.IsNullOrEmpty())
            {
                return null;
            }

            ViewWXUserInfo viewFWUser = JsonConvert.DeserializeObject<ViewWXUserInfo>(userStr);
            return viewFWUser;
        }
    }
}
